Skip to main content
ICT
Lesson A11 - Inheritance
 
Main Previous Next
Title Page >  
Summary >  
Lesson A1 >  
Lesson A2 >  
Lesson A3 >  
Lesson A4 >  
Lesson A5 >  
Lesson A6 >  
Lesson A7 >  
Lesson A8 >  
Lesson A9 >  
Lesson A10 >  
Lesson A11 >  
Lesson A12 >  
Lesson A13 >  
Lesson A14 >  
Lesson A15 >  
Lesson A16 >  
Lesson A17 >  
Lesson A18 >  
Lesson A19 >  
Lesson A20 >  
Lesson A21 >  
Lesson A22 >  
Lesson AB23 >  
Lesson AB24 >  
Lesson AB25 >  
Lesson AB26 >  
Lesson AB27 >  
Lesson AB28 >  
Lesson AB29 >  
Lesson AB30 >  
Lesson AB31 >  
Lesson AB32 >  
Lesson AB33 >  
Vocabulary >  
 

C. Using Inheritance page 5 of 10

  1. The following program uses a class Person to represent people you might find at a school. The Person class has basic information in it, such as name, age and gender. An additional class, Student, is created that is similar to Person, but has the Id number and grade point average of the student.

    public class Person{
      private String myName;   // name of the person
      private int myAge;       // person's age
      private String myGender; // "M" for male, "F" for female

      // constructor
      public Person(String name, int age, String gender){
        myName = name;
        myAge = age;
        myGender = gender;
      }

      public String getName(){
        return myName;
      }

      public int getAge(){
        return myAge;
      }

      public String getGender(){
        return myGender;
      }

      public void setName(String name){
        myName = name;
      }

      public void setAge(int age){
        myAge = age;
      }

      public void setGender(String gender){
        myGender = gender;
      }

      public String toString(){
        return myName + “, age: “ + myAge + “, gender: “
          + myGender;
      }
    }

    //-----------------End of Person Class-----------------//

public class Student extends Person{
  private String myIdNum; // Student Id Number
  private double myGPA; // grade point average

  // constructor
  public Student(String name, int age, String gender,
                                String idNum, double gpa){
    // use the super class' constructor
    super(name, age, gender);

    // initialize what's new to Student
    myIdNum = idNum;
    myGPA = gpa;
  }

  public String getIdNum(){
    return myIdNum;
  }

  public double getGPA(){
    return myGPA;
  }

  public void setIdNum(String idNum){
    myIdNum = idNum;
  }

  public void setGPA(double gpa){
    myGPA = gpa;
  }
}

//-----------------End of Student Class-----------------//

public class HighSchool{
  public static void main (String args[]){
    Person bob = new Person("Coach Bob", 27, "M");
    Student lynne = new Student("Lynne Brooke", 16, "F",
                                "HS95129", 3.5);
    System.out.println(bob);
    System.out.println(lynne);
    // The previous two lines could have been written as:
    // System.out.println(bob.toString());
    // System.out.println(lynne.toString());
  }
}

  1. The Student class is a derived class (subclass) of Person. An object of type Student contains myIdNum and myGPA, which are defined in Student. It also has indirect access to the private variables myName, myAge, and myGender from Person through the methods getName(), getAge(), getGender(), setName(), setAge(), and setGender() that it inherits from Person.

  2. The constructor for the Student class initializes the instance data of Student objects and uses the Person class’s constructor to initialize the data of the Person superclass. The constructor for the Student class looks like this:

    // constructor
    public Student(String name, int age, String gender,
                   String idNum, double gpa){
       // use the super class's constructor
       super(name, age, gender);

       // initialize what's new to Student
       myIdNum = idNum;
       myGPA = gpa;
    }

The statement super(name, age, gender) invokes the Person class’s constructor to initialize the inherited data in the superclass. The next two statements initialize the members that only Student has. Note that when super is used in a constructor, it must be the first statement.

  1. So far, we have only seen the public (class members that can be accessed outside the class) and private (class members that are inaccessible from outside the class) access modifiers. There is a third access modifier that can be applied to an instance variable or method. If it is declared to be protected, then it can be used in the class in which it is defined and in any subclass of that class. This declaration is less restrictive than private and more restrictive than public. The A.P. Java subset allows the use of protected with methods but discourages its use for instance variables. It is preferred that all instance variables are private. Indirect access from subclasses should be done with public "getter" and "setter" methods. While protected members are available to provide a foundation for the subclasses to build on, they are still invisible to the public at large.

 

Main Previous Next
Contact
 © ICT 2006, All Rights Reserved.